package core;


import fileoperation.FileDeletion;
import gitobject.Branch;
import gitobject.Head;
import repository.Repository;

import java.io.File;

public class JitBranch {

    public static String branchPath = Repository.getGitDir() + File.separator + "refs" + File.separator + "heads";

    public static void branch(String name) throws Exception {

        if(name.equals("")) {
            File fileDir = new File(branchPath);
            File fileList[] = fileDir.listFiles();
            Head head = Head.deserialize();
            //System.out.print(head.getBranchName());
            for (int i = 0; i < fileList.length; i++){
                if(fileList[i].getName().equals(head.getBranchName()))    System.out.print("*");
                else    System.out.print(" ");
                System.out.println(fileList[i].getName());
            }

        }
        else {
            File file = new File(branchPath + File.separator + name);
            if (!file.exists()) {
                Head head = Head.deserialize();
                Branch branch = new Branch(head.getKey(), name);

            } else {
                System.out.print("The branch already exists!");
            }
        }
    }

    public static void deleteBranch(String name) throws Exception {
        File file = new File(branchPath + File.separator + name);
        Head head = Head.deserialize();
        String branchName = head.getBranchName();
        if(name.equals(branchName)){
            System.out.println("不能删除当前所在的分支");
            return;
        }
        if(file.exists()){
            FileDeletion.deleteFile(file);
        }
    }


}
